package com.gton.commons;


import com.gton.commons.excep.DIYException;
import com.gton.commons.myenum.ExcptionEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @program: PersonalDesign
 * @description: 统一返回格式
 * @author: GuoTong
 * @create: 2020-11-12 17:09
 **/
/*统一返回格式*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ResultAdvice {

    private Integer code;//状态码

    private String msg;//返回的提示信息

    private Object data;//返回的数据

    /*无消息成功*/
    public static ResultAdvice OK(){
        return new ResultAdvice(200,"成功",null);
    }
    /*带回调消息成功*/
    public static ResultAdvice OK(Object data){
        return new ResultAdvice(200,"成功",data);
    }

    /*无消息成功*/
    public static ResultAdvice error(String msg,Object data){
        return new ResultAdvice(500,msg,data);
    }
    /*带回调自定义消息成功*/
    public static ResultAdvice error(String msg){
        return new ResultAdvice(500,msg,null);
    }


    //自定义异常返回的结果
    public static ResultAdvice diyExcption(DIYException de){
        ResultAdvice result = new ResultAdvice();
        result.setCode(de.getErrorCode());
        result.setMsg(de.getErrorMsg());
        result.setData(null);
        return result;
    }

    //其他异常处理方法返回的结果
    public static ResultAdvice otherError(ExcptionEnum errorEnum){
        ResultAdvice result = new ResultAdvice();
        result.setMsg(errorEnum.getErrorMsg());
        result.setCode(errorEnum.getErrorCode());
        result.setData(null);
        return result;
    }

    /*枚举错误*/
    public static ResultAdvice error(ExcptionEnum pwdOrUsernameError) {
        /*错误码、错误信息、消息*/
        return new ResultAdvice(pwdOrUsernameError.getErrorCode(),pwdOrUsernameError.getErrorMsg(),null);
    }
}
